import { Navigate, useLocation } from 'react-router-dom'
import { getStorage, setStorage } from '../utils/storage'
import { treeEach } from '../utils'
import { routes } from '../router'
let LAST_PATH_NAME = 'LAST_PATH_NAME'
const RouterAuth = ({ children }) => {
    const localtion = useLocation()
    setStorage(LAST_PATH_NAME, localtion.pathname)
    let token = getStorage('Token')
    if(!token) {
        return <Navigate to="/login" replace />
    }
    treeEach(routes,'children', 'path',item => {
        if(localtion.pathname.includes(item.path)) {
            document.title = item.title
        }
    })
    return children
}
const LoginRouterAuth = ({ children }) => {
    let token =  getStorage('Token')
    if(token) {
        return <Navigate to={ getStorage(LAST_PATH_NAME) } replace />
    }
    document.title = '登录页'
    return children
}

export {
    RouterAuth,
    LoginRouterAuth
}
